package org.example.mallchat.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.mallchat.common.domain.local.RequestInfo;
import org.example.mallchat.common.domain.vo.ApiResult;
import org.example.mallchat.common.thread.ThreadLocalHandler;
import org.example.mallchat.domain.vo.request.UserBlackReq;
import org.example.mallchat.domain.vo.request.UserNameModifyReq;
import org.example.mallchat.domain.vo.request.UserWearBadgeReq;
import org.example.mallchat.domain.vo.response.UserBadgeRep;
import org.example.mallchat.domain.vo.response.UserInfoRep;
import org.example.mallchat.exception.ErrorCode;
import org.example.mallchat.exception.ThrowUtils;
import org.example.mallchat.service.BlackService;
import org.example.mallchat.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author ZZH
 * @since 2024-12-18
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户表接口")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private BlackService blackService;

    @GetMapping("/public/test")
    public ApiResult<Void> test() {
        System.out.println("test");
        return ApiResult.success(null);
    }


    @GetMapping("/userInfo")
    @ApiOperation("获取用户个人信息")
    public ApiResult<UserInfoRep> getUserInfo() {
        RequestInfo requestInfo = ThreadLocalHandler.getRequestInfo();
        UserInfoRep user = userService.getUserInfo(requestInfo.getUid());
        return ApiResult.success(user);
    }

    @PutMapping("/modifyUserName")
    @ApiOperation("修改用户名")
    public ApiResult<Void> modifyUserName(@RequestBody UserNameModifyReq userNameModify) {
        ThrowUtils.throwIf(userNameModify == null || userNameModify.getUserName() == null, ErrorCode.PARAMS_ERROR);
        RequestInfo requestInfo = ThreadLocalHandler.getRequestInfo();
        userService.modifyUserName(requestInfo.getUid(), userNameModify.getUserName());
        return ApiResult.success(null);
    }

    @GetMapping("/badgeList")
    @ApiOperation("获取用户的徽章列表")
    public ApiResult<List<UserBadgeRep>> getBadgeList() {
        return ApiResult.success(userService.getBadgeList(ThreadLocalHandler.getRequestInfo().getUid()));
    }


    @PutMapping("/wearBadge")
    @ApiOperation("佩戴徽章")
    public ApiResult<Void> wearBadge(@RequestBody UserWearBadgeReq userWearBadgeReq) {
        ThrowUtils.throwIf(userWearBadgeReq == null || userWearBadgeReq.getWearingItemId() == null, ErrorCode.PARAMS_ERROR);
        return ApiResult.success(userService.wearBadge(ThreadLocalHandler.getRequestInfo().getUid(), userWearBadgeReq.getWearingItemId()));
    }


    @PutMapping("/black")
    @ApiOperation("拉黑用户")
    public ApiResult<Void> blackUser(@RequestBody UserBlackReq userBlackReq) {
        ThrowUtils.throwIf(userBlackReq == null || userBlackReq.getUid() == null, ErrorCode.PARAMS_ERROR);
        return ApiResult.success(blackService.blackUser(ThreadLocalHandler.getRequestInfo().getUid(), userBlackReq.getUid()));
    }

}

